<?php session_start();
ini_set('memory_limit', '256M');

$db_server = "localhost";
$db_name = "erepublik";
$db_user = "";
$db_passwd = "";

function caclutime(){ 
	$time = explode( " ", microtime()); 
	$usec = (double)$time[0]; 
	$sec = (double)$time[1]; 
	return $sec + $usec; 
}
$begin_time=caclutime();

//對資料庫連線
if(!@mysql_connect($db_server, $db_user, $db_passwd))
        die(trigger_error(mysql_error(),E_USER_ERROR));

mysql_query("SET NAMES utf8");
if(!@mysql_select_db($db_name))
        die("無法使用資料庫");

/////
function objectsIntoArray($arrObjData, $arrSkipIndices = array())
{
    $arrData = array();
    
    // if input is object, convert into array
    if (is_object($arrObjData)) {
        $arrObjData = get_object_vars($arrObjData);
    }
    
    if (is_array($arrObjData)) {
        foreach ($arrObjData as $index => $value) {
            if (is_object($value) || is_array($value)) {
                $value = objectsIntoArray($value, $arrSkipIndices); // recursive call
            }
            if (in_array($index, $arrSkipIndices)) {
                continue;
            }
            $arrData[$index] = $value;
        }
    }
    return $arrData;
}

$xmlUrl = "china_citizens.xml"; // XML feed file/URL
$xmlStr = file_get_contents($xmlUrl);
$xmlObj = simplexml_load_string($xmlStr);
$arrXml = objectsIntoArray($xmlObj);
$avatar_link = "avatar-link";
$is_organization = "is-organization";
$experience_points = "experience-points";
$military_skills = "military-skills";
$military_skill = "military-skill";
$rank_points = "rank-points";
$rank = "rank";

//print_r($arrXml);

	function cut_linker($link){
		$cut_link = substr($link, 53, -10);
		$fr = "http://static.erepublik.com/uploads/avatars/Citizens/";
		return $cut_link;
	}
	
	function rank_name($rankex){

	if ($rankex<10){
		$title="Recruit";
		$rank=1;
	}elseif ($rankex>=10 && $rankex<30){
		$title="Private";
		$rank=2;
	}
	elseif ($rankex>=30 && $rankex<60){
		$title="Private*";
		$rank=3;
	}
	elseif ($rankex>=60 && $rankex<100){
		$title="Private**";
		$rank=4;
	}
	elseif ($rankex>=100 && $rankex<150){
		$title="Private***";
		$rank=5;
	}
	elseif ($rankex>=150 && $rankex<250){
		$title="Corporal";
		$rank=6;
	}
	elseif ($rankex>=250 && $rankex<350){
		$title="Corporal*";
		$rank=7;
	}
	elseif ($rankex>=350 && $rankex<450){
		$title="Corporal**";
		$rank=8;
	}
	elseif ($rankex>=450 && $rankex<600){
		$title="Corporal***";
		$rank=9;
	}
	elseif ($rankex>=600 && $rankex<800){
		$title="Sergeant";
		$rank=10;
	}
	elseif ($rankex>=800 && $rankex<1000){
		$title="Sergeant*";
		$rank=11;
	}
	elseif ($rankex>=1000 && $rankex<1400){
		$title="Sergeant**";
		$rank=12;
	}
	elseif ($rankex>=1400 && $rankex<1850){
		$title="Sergeant***";
		$rank=13;
	}
	elseif ($rankex>=1850 && $rankex<2350){
		$title="Lieutenant";
		$rank=14;
	}
	elseif ($rankex>=2350 && $rankex<3000){
		$title="Lieutenant*";
		$rank=15;
	}
	elseif ($rankex>=3000 && $rankex<3750){
		$title="Lieutenant**";
		$rank=16;
	}
	elseif ($rankex>=3750 && $rankex<5000){
		$title="Lieutenant***";
		$rank=17;
	}
	elseif ($rankex>=5000 && $rankex<6500){
		$title="Captain";
		$rank=18;
	}
	elseif ($rankex>=6500 && $rankex<9000){
		$title="Captain*";
		$rank=19;
	}
	elseif ($rankex>=9000 && $rankex<12000){
		$title="Captain**";
		$rank=20;
	}
	elseif ($rankex>=12000 && $rankex<15500){
		$title="Captain***";
		$rank=21;
	}
	elseif ($rankex>=15500 && $rankex<20000){
		$title="Major";
		$rank=22;
	}
	elseif ($rankex>=20000 && $rankex<25000){
		$title="Major*";
		$rank=23;
	}
	elseif ($rankex>=25000 && $rankex<31000){
		$title="Major**";
		$rank=24;
	}
	elseif ($rankex>=31000 && $rankex<40000){
		$title="Major***";
		$rank=25;
	}
	elseif ($rankex>=40000 && $rankex<52000){
		$title="Commander";
		$rank=26;
	}
	elseif ($rankex>=52000 && $rankex<67000){
		$title="Commander*";
		$rank=27;
	}
	elseif ($rankex>=67000 && $rankex<85000){
		$title="Commander**";
		$rank=28;
	}
	elseif ($rankex>=85000 && $rankex<110000){
		$title="Commander***";
		$rank=29;
	}
	elseif ($rankex>=110000 && $rankex<140000){
		$title="LtColonel";
		$rank=30;
	}
	elseif ($rankex>=140000 && $rankex<180000){
		$title="LtColonel*";
		$rank=31;
	}
	elseif ($rankex>=180000 && $rankex<225000){
		$title="LtColonel**";
		$rank=32;
	}
	elseif ($rankex>=225000 && $rankex<285000){
		$title="LtColonel***";
		$rank=33;
	}
	elseif ($rankex>=285000 && $rankex<355000){
		$title="Colonel";
		$rank=34;
	}
	elseif ($rankex>=355000 && $rankex<435000){
		$title="Colonel*";
		$rank=35;
	}
	elseif ($rankex>=435000 && $rankex<540000){
		$title="Colonel**";
		$rank=36;
	}
	elseif ($rankex>=540000 && $rankex<660000){
		$title="Colonel***";
		$rank=37;
	}
	elseif ($rankex>=660000 && $rankex<800000){
		$title="General";
		$rank=38;
	}
	elseif ($rankex>=800000 && $rankex<950000){
		$title="General*";
		$rank=39;
	}
	elseif ($rankex>=950000 && $rankex<1140000){
		$title="General**";
		$rank=40;
	}
	elseif ($rankex>=1140000 && $rankex<1350000){
		$title="General***";
		$rank=41;
	}
	elseif ($rankex>=1350000 && $rankex<1600000){
		$title="FieldMarshal";
		$rank=42;
	}
	elseif ($rankex>=1600000 && $rankex<1875000){
		$title="FieldMarshal*";
		$rank=43;
	}
	elseif ($rankex>=1875000 && $rankex<2185000){
		$title="FieldMarshal**";
		$rank=44;
	}
	elseif ($rankex>=2185000 && $rankex<2250000){
		$title="FieldMarshal***";
		$rank=45;
	}
	elseif ($rankex>=2250000 && $rankex<3000000){
		$title="SupremeMarshal";
		$rank=46;
	}
	elseif ($rankex>=3000000 && $rankex<3500000){
		$title="SupremeMarshal*";
		$rank=47;
	}
	elseif ($rankex>=3500000 && $rankex<4150000){
		$title="SupremeMarshal**";
		$rank=48;
	}
	elseif ($rankex>=4150000 && $rankex<4900000){
		$title="SupremeMarshal***";
		$rank=49;
	}
	elseif ($rankex>=4900000 && $rankex<5800000){
		$title="NationalForce";
		$rank=50;
	}
	elseif ($rankex>=5800000 && $rankex<7000000){
		$title="NationalForce*";
		$rank=51;
	}
	elseif ($rankex>=7000000 && $rankex<9000000){
		$title="NationalForce**";
		$rank=52;
	}
	elseif ($rankex>=9000000 && $rankex<11500000){
		$title="NationalForce***";
		$rank=53;
	}
	elseif ($rankex>=11500000 && $rankex<14500000){
		$title="World ClassForce";
		$rank=54;
	}
	elseif ($rankex>=14500000 && $rankex<18000000){
		$title="WorldClassForce*";
		$rank=55;
	}
	elseif ($rankex>=18000000 && $rankex<22000000){
		$title="WorldClassForce**";
		$rank=56;
	}
	elseif ($rankex>=22000000 && $rankex<26500000){
		$title="WorldClassForce***";
		$rank=57;
	}
	elseif ($rankex>=26500000 && $rankex<31500000){
		$title="LegendaryForce";
		$rank=58;
	}
	elseif ($rankex>=31500000 && $rankex<37000000){
		$title="LegendaryForce*";
		$rank=59;
	}
	elseif ($rankex>=37000000 && $rankex<43000000){
		$title="LegendaryForce**";
		$rank=60;
	}
	elseif ($rankex>=43000000 && $rankex<50000000){
		$title="LegendaryForce***";
		$rank=61;
	}
	elseif ($rankex>=50000000 && $rankex<60000000){
		$title="GodofWar";
		$rank=62;
	}
	return $rank;
	}

$datacount = 0;
// the mani script to input the data
do{
	$if_org = $xmlObj->citizen[$datacount]->$is_organization;
	if($if_org == "false"){
		$name = addslashes($xmlObj->citizen[$datacount]->name);
		$id = $xmlObj->citizen[$datacount]->id;
		$level = $xmlObj->citizen[$datacount]->level;		
		$exp = $xmlObj->citizen[$datacount]->$experience_points;		
		$citizenship = $xmlObj->citizen[$datacount]->citizenship->country->id;
		$strength = $xmlObj->citizen[$datacount]->$military_skills->$military_skill->points;		
			$military_rank_points = $xmlObj->citizen[$datacount]->military->$rank_points;		
		$military_rank = rank_name($military_rank_points);
			$avatar = $xmlObj->citizen[$datacount]->$avatar_link;
		$avatar_link_o = cut_linker($avatar);
		
		
		$sql = "SELECT * FROM list where id = '$id'";
		$result = mysql_query($sql);
		$row = @mysql_fetch_row($result);

	if($row[0] != NULL) {
		$Result1 = mysql_query("UPDATE list SET name = '$name', level = '$level', exp = '$exp',citizenship = '$citizenship',strength = '$strength',military_rank = '$military_rank',avatar_link = '$avatar_link_o' WHERE id = '$id'") or die(mysql_error());
		}else {
		$Result1 = mysql_query("INSERT list SET name = '$name', id = '$id', level = '$level', exp = '$exp',citizenship = '$citizenship',strength = '$strength',military_rank = '$military_rank',avatar_link = '$avatar_link_o' ") or die(mysql_error());
		}
		
	}
	$datacount++;	
	$aa = $xmlObj->citizen[$datacount];
  }while (isset($aa) == 1);
  
$end_time=caclutime();
$total=$end_time-$begin_time;
echo "Total Run Time: $total s "; 

mysql_free_result($result);
?>